4  Making Tables

As you create reproducible analyses, you’ll want to incorporate data visualizations such as tables. You can make use of the code you’ve learned this semester inside Quarto documents. The new aspect is to capitalize on the appropriate code chunk options to take your visualizations to the next level.

4.1 Using kable in Quarto

To demonstrate making a table, I’m going to use the Diamond Summary Table. If you look in the Code Appendix, you’ll find the code to wrangle the Diamonds data (Wickham, 2015) but that code doesn’t appear in the body of this guide. Listing 4.1 shows the code block for creating a table in a Quarto document.

Listing 4.1: Code Chunk for Making Table
```{r}
#| label: tbl-diamondsSummary
#| tbl-cap: "Summary Statistics on Diamond Width by Cut"
#| tbl-pos: H
#| html-table-processing: none
#| lst-label: lst-table1
#| lst-cap: "Code Chunk for Making Table"
# Make Diamond Summary Table ----
summaryWidth |>
  kable(
    digits = 2,
    col.names = c("Cut Quality", "Freq.", "Min", "20%-tile", "40%-tile", "Median",
                  "60%-tile", "80%-tile", "Max", "SAM", "SASD"),
    format.args = list(big.mark = ","),
    align = "lcccccccccc"
  ) |>
  kable_classic() |>
  add_footnote(
    label = c("Width was measured in milimeters."),
    notation = "none",
    threeparttable = TRUE
  )
```
Table 4.1: Summary Statistics on Diamond Width by Cut
Cut Quality Freq. Min 20%-tile 40%-tile Median 60%-tile 80%-tile Max SAM SASD
Fair 1,609 3.78 5.50 5.98 6.10 6.23 7.00 10.54 6.19 0.94
Good 4,904 3.84 4.74 5.63 5.99 6.23 6.60 9.38 5.85 1.05
Very Good 12,081 3.71 4.62 5.40 5.77 6.16 6.68 9.94 5.77 1.10
Premium 13,789 3.68 4.66 5.62 6.06 6.40 6.94 58.90 5.95 1.26
Ideal 21,550 3.73 4.46 4.97 5.26 5.72 6.57 31.80 5.52 1.07
Width was measured in milimeters.

There a couple of items to notice in Listing 4.1.

  1. The first two chunk options, label and tbl-cap are absolutely necessary if you want to be to cross-reference your tables.
    1. The value for label MUST begin with tbl-.
    2. The value for tbl-caption would be what you would have put as the value for the caption argument in the kable call. You do not need both.
  2. When using the {kableExtra} package, it is a good practice to also use the code chunk option html-table-processing: none. This will keep Quarto from trying to re-process the table, which can lead to odd or wonky looking tables.
TipBest Practice

Only put the code for one table in each code chunk. That is, use separate code chunks for each table that you want to create. This will help ensure that each table gets appropriately created, captioned, and cross-referenced correctly.

4.2 Table Positioning

When rendering the HTML, your tables will appear in same relative place in the document as the creating code chunk. The same isn’t true when you render to paginated documents (PDFs or Word). By default, Quarto’s engine will attempt to place the table as close the relative location of the code chunk as possible but the table may end up on another page. To override this and “force” Quarto to put the table where you want, you can use the tbl-pos: H chunk option (tbl-pos for “table position” and H for “Here!”). Keep in mind that this option will be ignored if space that is “here” isn’t large enough for your table.

4.3 Wide Tables

If you have a wide table (i.e., extends past the right-hand margin of the page), you can look into using the following command from {kableExtra}: kable_styling(latex_options = "scale_down"). This should be partnered with the argument format = "latex" in the preceding kable call. The scale down option will automatically adjust the table’s font size and spacing to get it to fit within the width of the page.

4.4 Alt Text and Tables

Tables do not generally need alternative text. If you follow the processes we’ve discussed in class, your tables will be in good shape for meeting accessibility needs.

This said, you still need to make sure that you discuss every table you include in your report in the narrative text. Any table you don’t discuss should be removed from your report.